package fileattrfilter

import (
	"strings"

	"gitee.com/guoliben/kydlp-doctotext/utils/ctrlledprint"
)

const DocTypeOleStorage = "application/x-ole-storage"

type FiletypeFilterRuleObject struct {
	MimetypeList  string
	ExtensionList string
	Exception     bool
}

type FiletypeObject struct {
	Extension       string
	Mimetype        string
	MimetypeAliases string
}

type FiletypeFilterRule struct {
	Summary FiletypeFilterRuleObject
	Body []FiletypeObject
}

var filetypefilterrule = FiletypeFilterRule{
	Summary: FiletypeFilterRuleObject{
		MimetypeList: "",
		ExtensionList: ".txt;.html;.xml;.json;.pdf;.doc;.xls;.ppt;.docx;.xlsx;.pptx;.bmp;.jpg;.png;.gif;.tiff;.tar;.gz;.zip;.rar;.7z",
		Exception: false,
	},
	Body: []FiletypeObject{
		{Extension: ".txt", Mimetype: "text/plain", MimetypeAliases: "text/plain; charset=utf-8"},
		{Extension: ".html", Mimetype: "text/html", MimetypeAliases: "ext/html; charset=utf-8"},
		{Extension: ".xml", Mimetype: "text/xml", MimetypeAliases: "text/xml; charset=utf-8"},
		{Extension: ".json", Mimetype: "application/json", MimetypeAliases: ""},
		{Extension: ".pdf", Mimetype: "application/pdf", MimetypeAliases: "application/x-pdf"},
		{Extension: ".doc", Mimetype: "application/vnd.ms-word", MimetypeAliases: "application/msword"},
		{Extension: ".xls", Mimetype: "application/vnd.ms-excel", MimetypeAliases: "application/msexcel"},
		{Extension: ".ppt", Mimetype: "application/vnd.ms-powerpoint", MimetypeAliases: "application/mspowerpoint"},
		{Extension: ".docx", Mimetype: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", MimetypeAliases: ""},
		{Extension: ".xlsx", Mimetype: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", MimetypeAliases: ""},
		{Extension: ".pptx", Mimetype: "application/vnd.openxmlformats-officedocument.presentationml.presentation", MimetypeAliases: ""},
		{Extension: ".bmp", Mimetype: "image/bmp", MimetypeAliases: "image/x-bmp, image/x-ms-bmp"},
		{Extension: ".jpg", Mimetype: "image/jpeg", MimetypeAliases: ""},
		{Extension: ".png", Mimetype: "image/png", MimetypeAliases: ""},
		{Extension: ".png", Mimetype: "image/vnd.mozilla.apng", MimetypeAliases: ""},
		{Extension: ".gif", Mimetype: "image/gif", MimetypeAliases: ""},
		{Extension: ".tiff", Mimetype: "image/tiff", MimetypeAliases: ""},
		{Extension: ".tar", Mimetype: "application/x-tar", MimetypeAliases: ""},
		{Extension: ".gz", Mimetype: "application/gzip", MimetypeAliases: "application/x-gzip, application/x-gunzip, application/gzipped, application/gzip-compressed, application/x-gzip-compressed, gzip/document"},
		{Extension: ".zip", Mimetype: "application/zip", MimetypeAliases: "application/x-zip, application/x-zip-compressed"},
		{Extension: ".rar", Mimetype: "application/x-rar-compressed", MimetypeAliases: "application/x-rar"},
		{Extension: ".7z", Mimetype: "application/x-7z-compressed", MimetypeAliases: ""},
	},
}

var filetypefilterrule_exception FiletypeFilterRule

func FilterIncludeFiletype(filetype, extension string) bool {
	ctrlledprint.Println("过滤检测文件类型...")

	if filetype == DocTypeOleStorage && extension != ".msi" {
		ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "触发了文件类型检测规则，将对其进行内容检测！")
		return true
	}

	triggered := false

	if len(extension) > 0 {
		if !filetypefilterrule.Summary.Exception {
			triggered = true
			for _, filetypeobj := range filetypefilterrule.Body {
				if filetypeobj.Extension == extension {
					if filetypeobj.Mimetype == filetype {
						ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "触发了文件类型检测规则，将对其进行内容检测！")
						return true
					} else {
						mimetypealiases := strings.Split(filetypeobj.MimetypeAliases, ", ")
						for _, mimetypealiase := range mimetypealiases {
							if mimetypealiase == filetype {
								ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "触发了文件类型检测规则，将对其进行内容检测！")
								return true
							}
						}
					}
				}
			}
		}
	} else {
		if !filetypefilterrule.Summary.Exception {
			triggered = true
			for _, filetypeobj := range filetypefilterrule.Body {
				if filetypeobj.Mimetype == filetype {
					ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "触发了文件类型检测规则，将对其进行内容检测！")
					return true
				} else {
					mimetypealiases := strings.Split(filetypeobj.MimetypeAliases, ", ")
					for _, mimetypealiase := range mimetypealiases {
						if mimetypealiase == filetype {
							ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "触发了文件类型检测规则，将对其进行内容检测！")
							return true
						}
					}
				}
			}
		}
	}

	if triggered {
		ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "已配置文件类型检测规则，但未被触发，将对其进行放行处理，不做内容检测！")
		return false
	} else {
		ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "未配置文件类型检测规则，将对所有类型进行内容检测！")
		return true
	}
}

func FilterExcludeFiletype(filetype, extension string) bool {
	ctrlledprint.Println("过滤例外文件类型...")

	if filetype == DocTypeOleStorage && extension == ".msi" {
		ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "触发了文件类型例外规则，将对其进行放行处理，不做内容检测！")
		return true
	}

	triggered := false

	if len(extension) > 0 {
		if filetypefilterrule_exception.Summary.Exception {
			triggered = true
			for _, filetypeobj := range filetypefilterrule_exception.Body {
				if filetypeobj.Extension == extension {
					if filetypeobj.Mimetype == filetype {
						ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "触发了文件类型例外规则，将对其进行放行处理，不做内容检测！")
						return true
					} else {
						mimetypealiases := strings.Split(filetypeobj.MimetypeAliases, ", ")
						for _, mimetypealiase := range mimetypealiases {
							if mimetypealiase == filetype {
								ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "触发了文件类型例外规则，将对其进行放行处理，不做内容检测！")
								return true
							}
						}
					}
				}
			}
		}
	} else {
		if filetypefilterrule_exception.Summary.Exception {
			triggered = true
			for _, filetypeobj := range filetypefilterrule_exception.Body {
				if filetypeobj.Mimetype == filetype {
					ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "触发了文件类型例外规则，将对其进行放行处理，不做内容检测！")
					return true
				} else {
					mimetypealiases := strings.Split(filetypeobj.MimetypeAliases, ", ")
					for _, mimetypealiase := range mimetypealiases {
						if mimetypealiase == filetype {
							ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "触发了文件类型例外规则，将对其进行放行处理，不做内容检测！")
							return true
						}
					}
				}
			}
		}
	}

	if triggered {
		ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "已配置文件类型例外规则，但未被触发，将对其进行内容检测！")
		return false
	} else {
		ctrlledprint.Println("文件类型:", filetype, "后缀:", extension, "未配置文件类型例外规则，将对所有类型进行内容检测！")
		return false
	}
}
